package cn.gailvlun.gll.presentation.chat;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.github.markzhai.recyclerview.BaseViewAdapter;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.chatlib.bean.SystemNotification;
import cn.gailvlun.gll.databinding.ActivitySystemNotificationBinding;
import cn.gailvlun.gll.presentation.apartment.ApartmentDetailActivity;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.chat.adapter.SystemNotificationAdapter;
import cn.gailvlun.gll.presentation.chat.vm.SystemNotificationActivityViewModel;
import cn.gailvlun.gll.presentation.emotion.EmotionDetail2Activity;
import cn.gailvlun.gll.presentation.emotion.bean.EmotionDetailTransfer;
import cn.gailvlun.gll.util.DateUtil;
import cn.gailvlun.gll.util.ProfileUtil;
import cn.gailvlun.gll.widget.CommonItemDecoration;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.functions.Consumer;
import io.realm.RealmResults;



public class SystemNotificationActivity extends BaseActivity{

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, SystemNotificationActivity.class);
        context.startActivity(intent);
    }

    public static void openFromNotification(Context context) {
        Intent intent = new Intent(context, SystemNotificationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    private ActivitySystemNotificationBinding mBinding;

    private SystemNotificationAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    private SystemNotificationActivityViewModel mViewModel;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_system_notification);

        mAdapter = new SystemNotificationAdapter(mContext);
        mAdapter.setPresenter(new Presenter());
        mBinding.recyclerView.setAdapter(mAdapter);

        mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);

        mBinding.recyclerView.addItemDecoration(new CommonItemDecoration(ConvertUtils.dp2px(73f), 0, ConvertUtils.dp2px(1), getResources().getColor(R.color.paleGreyFour), Color.WHITE, false));

        mViewModel = ViewModelProviders.of(this).get(SystemNotificationActivityViewModel.class);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {


        mViewModel.readAllNotification();
        mViewModel.getSystemNotificationFlowable()
                .subscribe(new Consumer<RealmResults<SystemNotification>>() {
                    @Override
                    public void accept(RealmResults<SystemNotification> systemNotifications) throws Exception {
                        mAdapter.clear();
                        for (SystemNotification systemNotification: systemNotifications) {
                            mAdapter.add(systemNotification, systemNotification.getType().ordinal());
                        }
                    }
                });
    }

    public class Presenter implements BaseViewAdapter.Presenter {
        public void onClick(SystemNotification item) {
            switch (item.getType()) {
                case emotionHeart:
                case emotionComment: {
                    EmotionDetailTransfer transfer = new EmotionDetailTransfer();
                    transfer.setId(item.getEmotion().getId());
                    Integer userId = ProfileUtil.getUserId();
                    if (userId != null && item.getEmotion().getOwner().getId() == userId) {
                        transfer.setTitle(R.string.emotion_my_emotion);
                    }
                    EmotionDetail2Activity.openActivity(mContext, transfer);
                    break;
                }
                case wishDeadLine: {

                    break;
                }
                case hotelHeart: {

                    break;
                }
                case roomOpen:
                case roomWillBeClosed:
                case roomClosed:
                case roomReviewed:
                case roomAbortion:
                case roomStartSign:{
                    ApartmentDetailActivity.openFromNotification(mContext,item.getRoom().getId());
                    break;
                }
            }
        }
    }

    public static class Util {
        public static String getHeader(SystemNotification systemNotification) {
            switch (systemNotification.getType()) {
                case emotionHeart:
                case emotionComment:
                case wishDeadLine:
                case hotelHeart:
                case hotelComment:
                case roomOpen:
                case roomWillBeClosed:
                case roomClosed:
                case roomReviewed:
                case roomAbortion:
                case roomStartSign:{
                    return systemNotification.getFrom().getAvatar();
                }
            }
            return null;
        }

        public static String getTitle(SystemNotification systemNotification) {
            switch (systemNotification.getType()) {
                case emotionHeart:
                case emotionComment:
                case wishDeadLine:
                case hotelHeart:
                case hotelComment:
                case roomOpen:
                case roomWillBeClosed:
                case roomClosed:
                case roomReviewed:
                case roomAbortion:
                case roomStartSign:{
                    return systemNotification.getFrom().getName();
                }
            }
            return null;
        }

        public static String getContent(SystemNotification systemNotification) {
            switch (systemNotification.getType()) {
                case roomOpen:
                case roomWillBeClosed:
                case roomClosed:
                case roomReviewed:
                case roomAbortion:
                case roomStartSign:{
                    return systemNotification.getText();
                }
            }
            return null;
        }

        public static String getDate(SystemNotification systemNotification) {
            return DateUtil.format(DateUtil.DOT_NO_SECOND_PATTERN, systemNotification.getCreated());
        }

        public static String getPic(SystemNotification systemNotification) {
            switch (systemNotification.getType()) {
                case emotionHeart:
                case emotionComment: {
                    if (systemNotification.getEmotion().getPics().size() > 0) {
                        return systemNotification.getEmotion().getPics().get(0);
                    }
                }
            }
            return null;
        }

        public static int getPicVisibility(SystemNotification systemNoti) {
            switch (systemNoti.getType()) {
                case emotionHeart:
                case emotionComment: {
                    return systemNoti.getEmotion().getPics().size() == 0 ? View.GONE : View.VISIBLE;
                }
                case roomOpen:
                case roomWillBeClosed:
                case roomClosed:
                case roomReviewed:
                case roomAbortion:{
                    return View.VISIBLE;
                }
                default: {
                    return View.GONE;
                }
            }
        }

        public static String getComment(SystemNotification systemNotification) {
            switch (systemNotification.getType()) {
                case emotionComment:
                case hotelComment: {
                    return systemNotification.getComment().getText();
                }
                case roomOpen:
                case roomWillBeClosed:
                case roomClosed:
                case roomReviewed:
                case roomAbortion:
                case roomStartSign:{
                    return systemNotification.getRoom().getPic();
                }
            }
            return null;
        }
    }
}
